<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/3/15
 * Time: 16:37
 */

namespace app\api\model;



class Coupon extends BaseModel
{
    protected  $autoWriteTimestamp = true;

    public function getGroundingTimeAttr($data)
    {
        return empty($data)?"":date('Y-m-d H:i:s',$data);
    }

    /**
     * 与work User多对一关系
     * @return \think\model\relation\BelongsTo
     */
    public function user(){
        return $this->belongsTo('WorkUser','own_user','id');
    }


    /**
     * 添加购物券
     * @param $data
     * @return false|int
     */
    public function addCoupon($data){
        return self::insert($data,'',true);
    }
    /**
     * 更新优惠券
     * @param $data
     * @return $this
     */
    public function updateCoupon($data){
        return self::update($data,['id'=>$data['id']]);
    }

    /**
     * 删除优惠卷
     * @param $id
     * @return int
     */
    public function deleteCoupon($id){
        return self::where('id','=',$id)->delete();
    }

    /**
     * 获取优惠卷详情
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponById($id){
        return self::where('id','=',$id)->find();
    }

    /**
     * 获取优惠卷列表
     * @param $t_id
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponList($t_id,$where){
        $result =  self::where('create_user','=',$t_id)
                ->where($where)
                ->with(['user'])
                ->select();
        foreach ($result as $item){
            if (!empty($item->user)){
                $item->user->visible(['id','name','position']);
            }
        }
        return $result;
    }

    /**
     * 获取用户自己的优惠卷
     * @param $t_id
     * @param $wu_id
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUserCoupon($t_id,$wu_id,$where){
        return self::where('create_user','=',$t_id)
            ->where('own_user','=',$wu_id)
            ->where($where)
            ->select();
    }


    /**
     * 批量更新优惠券
     * @param $ids
     * @param $data
     * @return $this
     */
    public function batchUpdateCoupon($ids,$data){
        return self::where('id','=',$ids)->update($data);
    }

    /**
     * 领卷数量自增
     * @param $id
     * @return int|true
     * @throws \think\Exception
     */
    public static function incNum($id){
        return self::where('id','=',$id)->setInc('take_count');
    }











}